#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2022-08-08 11:49:10
# @author: 鹰眼测试

"""
SimpleAPI -> user.conf.yaml：存放用户配置的数据
ReadUserConf专门用来检查、读取用户配置
"""


from SystemFile.pathEnum import get_path
from Utils.dataUtils.yamlUtility import YAMLOption


class ReadUserConf:
    def __init__(self):
        # self.user_conf_path：配置文件路径
        self.user_conf_path = get_path("user")
        # self.user_conf：整个配置文件的数据
        self.user_conf = YAMLOption(self.user_conf_path).read_yaml()

    def read_evn(self):
        """
        raise KeyError：检查evn字段是否有被修改。配置文件中evn字段名是不允许修改的
        :return: 返回读取到的evn配置
        """
        try:
            return self.user_conf["evn"]
        except KeyError:
            raise KeyError(f"{self.user_conf_path} 用户配置文件中 'evn' 字段名不允许修改")

    def read_host_key(self):
        """
        :return: 返回本次读取的测试环境
        """
        evn = self.read_evn()
        try:
            return evn["host"]
        except KeyError:
            raise KeyError(f"{self.user_conf_path} 用户配置文件中 'evn->host' 字段名不允许修改")

    def read_host(self):
        """
        :return: 返回对应环境的host
        """
        evn = self.read_evn()
        host_key = self.read_host_key()
        try:
            return evn[host_key]
        except KeyError:
            raise KeyError(f"{self.user_conf_path} 没有找到'{host_key}'环境，请检查 'evn->host' 是否输入是否正确")
